/**随机生成N个商品 */
const createGoods = function(){
    let index = 1;
    return (num)=>{
        let goodsArr = [];
        for(let i = 0;i < num;i++){
            let name = '';
            for(let i = 0;i < (5 + Math.random() * 5);i++){
                name += String.fromCharCode(Math.floor(Math.random() * 25 + 97));
                if(i == 0){
                    name = name.toUpperCase();
                }
            }
            const oldPrice = Math.floor(Math.random() * 280 + 20);
            const newPrice = Math.floor(oldPrice * (Math.random() + 1) / 2 )
            goodsArr.push(new GoodsInfo({
                id:index,
                classified:'Test',
                name:name,
                url:'assets/imgs/shop/sunny.svg',
                starNum:Math.floor(Math.random() * 6),
                downloadNum:Math.floor(Math.random() * 1000),
                newPrice:newPrice,
                oldPrice:oldPrice
            }))
            index ++;
        }
        return goodsArr;
    }
}()

/**商品信息 */
class GoodsInfo{
    id;
    classified;
    name;
    url;//imgUrl
    starNum;
    downloadNum;
    newPrice;
    oldPrice;
    date;
    defaultUrl = 'assets/imgs/shop/application.svg';
    loadTip = '加载中';
    time;//折扣剩余时间

    createBlank(){
        return this.create(-1,this.loadTip,this.loadTip,this.defaultUrl,5,this.loadTip,this.loadTip,this.loadTip);
    }

    createItem(){
        return this.create(this.id,this.classified,this.name,this.url,this.starNum,this.downloadNum,this.newPrice,this.oldPrice);
    }

    createSimpleBlank(){
        return this.createSimple(-1,this.loadTip,this.defaultUrl,5,this.loadTip,this.loadTip);
    }

    createSimpleItem(){
        return this.createSimple(this.id,this.name,this.url,this.starNum,this.newPrice,this.oldPrice);
    }

    /**对标 首页 -> 下载量排行 -> 商品信息 */
    create(id,classified,name,url,starNum,downloadNum,newPrice,oldPrice){
        return    `
        <div class="col-lg-1-5 col-md-4 col-12 col-sm-6">
            <div class="product-cart-wrap mb-30">
                <div class="product-img-action-wrap">
                    <div class="product-img product-img-zoom">
                        <a href="shop-product-full.html">
                            <img class="default-img" src="${ htmlEscape(url) }" alt="">
                        </a>
                    </div>
                    <div class="product-action-1">
                        <a aria-label="加入心愿单" class="action-btn" href="shop-wishlist.html"><i class="fi-rs-heart"></i></a>
                        <a aria-label="查看详情" class="action-btn" data-bs-toggle="modal" data-bs-target="#quickViewModal"><i class="fi-rs-eye"></i></a>
                    </div>
                    ${ newPrice == oldPrice ? '' :
                        '<div class="product-badges product-badges-position product-badges-mrg">' +
                            '<span class="best">-' + (parseInt( (oldPrice - newPrice) / oldPrice * 100) > 0 ? parseInt( (oldPrice - newPrice) / oldPrice * 100) : 1) + '%</span>' +
                        '</div>'
                    }
                </div>
                <div class="product-content-wrap">
                    <div class="product-category">
                        <a href="shop-grid-right.html">${ htmlEscape(classified) }</a>
                    </div>
                    <h2>
                        <a href="shop-product-full.html">${ htmlEscape(name) }</a>
                    </h2>
                    <div class="product-rate-cover">
                        <div class="product-rate d-inline-block">
                            <div class="product-rating" style="width:${ 20 * htmlEscape(starNum) }%"></div>
                        </div>
                        <span class="font-small ml-5 text-muted"> (${ htmlEscape(starNum) }.0)</span>
                    </div>
                    <div>
                        <span class="font-small text-muted">下载量${ htmlEscape(downloadNum) }</span>
                    </div>
                    <div class="product-card-bottom">
                        <div class="product-price">
                            <span>￥${ htmlEscape(newPrice) }</span>
                            ${ newPrice == oldPrice ? '' : '<span class="old-price">￥' + htmlEscape(oldPrice) + '</span>' }
                        </div>
                        <div class="add-cart">
                            <a class="add" href="shop-cart.html"><i class="fi-rs-shopping-cart mr-5"></i>加入购物车</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        `
    }

    /**对标 首页 -> 各大榜单 -> 商品信息 */
    createSimple(id,name,url,starNum,newPrice,oldPrice){
        return `
        <article class="row align-items-center hover-up">
            <figure class="col-md-4 mb-0">
                <a href="shop-product-full.html"><img src="${ htmlEscape(url) }" alt=""></a>
            </figure>
            <div class="col-md-8 mb-0">
                <h6>
                    <a href="shop-product-full.html">${ htmlEscape(name) }</a>
                </h6>
                <div class="product-rate-cover">
                    <div class="product-rate d-inline-block">
                        <div class="product-rating" style="width:${ 20 * htmlEscape(starNum) }%"></div>
                    </div>
                    <span class="font-small ml-5 text-muted"> (${ htmlEscape(starNum) }.0)</span>
                </div>
                <div class="product-price">
                    <span>${ htmlEscape(newPrice) }</span>
                    ${ newPrice == oldPrice ? '' : '<span class="old-price">￥' + htmlEscape(oldPrice) + '</span>' }
                </div>
            </div>
        </article>
        `
    }

    constructor(data){
        for(let key in data){
            this[key] = data[key];
        }
        if(this.starNum !== undefined && this.starNum < 1){
            this.starNum = 1;
        }
    }
}

/**return str */
eval(function(p,a,c,k,e,r){e=function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'[4-8bce-hj-mo-rt-zA-E]'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(j(s){c f={},n="",o="",r="",w=/^([0-9]{1,}([.][0-9]*)?)$/;f.e={k:["zero","one","two","three","four","five","six","seven","eight","nine"],x:["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"],y:["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],t:["hundred","thousand","million","billion","trillion","quadrillion"]};f.u=j(n){try{c 7=5.z(n).p(\',\');4="";for(c i=0;i<7.8;i++){6(7[i].8==3){4+=5.e.k[7[i].v(0,1)]+" ";4+=5.e.t[0];6(5.q(7[i].v(1))!=""){4+=" and "+5.q(7[i].v(1))}}l 6(7[i].8==2){4+=5.q(7[i])}l 6(7[i].8==1){4+=5.e.k[7[i]]}6(i<7.8-1){4+=" "+5.e.t[7.8-1-i]+" "}}g 4}catch(A){console.error(A)}};f.B=j(n){c d=n.p(\'\');c 4=\'\';6(d.8>0){d.forEach(a=>{4+=5.u(a)+" "})}g 4};f.q=j(n){c 4="";6(C(n)!=0){c h=n.p(\'\');6(h[0]<1){4=5.e.k[h[1]]}l 6(h[0]==1){4=5.e.x[h[1]]}l{4=5.e.y[h[0]-2]+"-"+5.e.k[h[1]]}}g 4};f.z=j(b){c b=(b||0).D(),4=\'\';while(b.8>3){4=\',\'+b.E(-3)+4;b=b.E(0,b.8-3)}6(b){4=b+4}g 4};s.prototype.toEnglish=j(){n=5;6(!w.test(C(n))){g"Error：Must in digital format"}c m=n.D().p(\'.\'),r=f.u(m[0]);6(m.8==2){6(m[1].8<=2){o=f.B(m[1])}l{g n}}g r+(o==""?"":" point "+o)}})(String)',[],41,'||||result|this|if|fenge|length|||num|var||tally|NumtoEnglish|return|dd||function|arr1|else|NumList||xiao|split|doubledight|zheng||arr4|Convert_integer|substring|regxinteger|arr2|arr3|toThousands|ex|Convert_decimal|parseInt|toString|slice'.split('|'),0,{}))
/**param str return num */
function convertToNum(english){
    let index = -1;
    while(index <= 100){
        index++;
        if(english == index.toString().toEnglish())
            break;
    }
    return index
}

/*字符转义 -> 可以偷懒 -> 一个个添加text的话太麻烦 且标签嵌套太多导致text的写法难以阅读*/
function htmlEscape(str) {
    let escapesMap = {
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#039;'
    },
    reUnescapedHtml = new RegExp(/[<>"']/g);
    return (str && reUnescapedHtml.test(str)) ? str.replace(reUnescapedHtml, function(chr) {
        return escapesMap[chr];
    }) : (str || "");
}

/*字符反转义 -> 如果后端需要 可以将已经转义的字符重新转回html*/
function htmlUnEscape(str) {
    let unescapes = {
        '&amp;': '&',
        '&lt;': '<',
        '&gt;': '>',
        '&quot;': '"',
        '&#39;': "'"
    },
    reEscapedHtml = new RegExp(/&(?:amp|lt|gt|quot|#39);/g);
    return (str && reEscapedHtml.test(str)) ? str.replace(reEscapedHtml, function(entity) {
        return unescapes[entity];
    }) : (str || '');
}

/**模拟请求 -> 接口写好 -> 搜索simulationRequest替换 */
function simulationRequest(func){
    return new Promise((resolve)=>{
        setTimeout(() => {
            const result = func();
            resolve({code:200,data:result,message:'success'});
        }, Math.floor(Math.random() * 1500 + 500));
    })
};